﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LibrarySystem
{
    public partial class WebForm9 : System.Web.UI.Page
    {
        private List<BusinessLayer.Book> result;
        private string _isbn;

        protected void Page_Load(object sender, EventArgs e)
        {
            _isbn = Request.QueryString["isbn"];
            if (_isbn != "")
            {
                result = BusinessLayer.Book.getByISBNorTitle("ISBN", _isbn);

                if (result.Count != 0)
                {
                    bookDetailTitle.Text = result[0].Title;
                    bookDetailAuthor.Text = "Författare: " + result[0].Author;
                    bookDetailISBN.Text = "ISBN: " + result[0].ISBN;
                    bookDetailLibNo.Text = "LibNo: " + result[0].LibNo.ToString();
                    bookDetailSignum.Text = "Klassifikation: " + result[0].Signum.ToString();
                    bookDetailCopyCount.Text = "Antal kopior: " + result.Count;

                    if (result[0].PublicationYear != null && result[0].PublicationYear != "")
                        bookDetailPublicationYear.Text = "Publiceringsår: " + result[0].PublicationYear;
                    if (result[0].Publisher != "0" && result[0].Publisher != null)
                        bookDetailPublisher.Text = "Publicist: " + result[0].Publisher;

                    bookDetailCopyList.DataSource = result;
                    bookDetailCopyList.DataBind();
                }

            }

        }

        protected void adminCtrlModify_Click(object sender, EventArgs e)
        {
            if (adminModify.Visible == false)
            {
                BusinessLayer.Book thisBook = new BusinessLayer.Book();
                thisBook = BusinessLayer.Book.getByISBNAdmin(_isbn);
                if (thisBook != null)
                {
                    modifyTitle.Text = thisBook.Title;
                    modifyISBN.Text = thisBook.ISBN;
                    modifyLibNo.Text = thisBook.LibNo.ToString();
                    modifyPublicationYear.Text = thisBook.PublicationYear;
                    modifyPublisher.Text = thisBook.Publisher;
                    modifySignum.Text = thisBook.SignId.ToString();
                   
                    adminModify.Visible = true;
                }
            }
            else
                adminModify.Visible = false;            
        }

        protected void adminCtrlRemove_Click(object sender, EventArgs e)
        {
            BusinessLayer.Book.DeleteBook(_isbn);
            Response.Redirect("adminSearch.aspx?val=bkTitle&term=");
        }

        protected void adminCtrlSave_Click(object sender, EventArgs e)
        {
            BusinessLayer.Book.ModBook(_isbn, modifyISBN.Text, modifyTitle.Text, Convert.ToInt32(modifySignum.Text), modifyPublicationYear.Text, modifyPublisher.Text, Convert.ToInt32(modifyLibNo.Text));
            Response.Redirect(Request.RawUrl);
        }
    }
}